/**
 * 
 */
package org.algos.tree;

/**
 * IntervalNode is the node for the interval tree
 * and it holds the interval and pointers to
 * left and right children. It also has the
 * max value among its children and the max height among left & right subtrees
 * 
 * @author Sreehari
 *
 */
public class IntervalNode<V extends Comparable<V>> {

	//Interval value
	private Interval<V> value;
	
	//Pointer to left node
	private IntervalNode<V> left;
	
	//Pointer to right node
	private IntervalNode<V> right;
	
	//Max value among the left & right subtrees
	private V max;
	
	//max height of (left subtree & right subtree)
	private int height;
	
	public Interval<V> getValue() {
		return value;
	}

	public void setValue(Interval<V> value) {
		this.value = value;
	}

	public IntervalNode<V> getLeft() {
		return left;
	}

	public void setLeft(IntervalNode<V> left) {
		this.left = left;
	}

	public IntervalNode<V> getRight() {
		return right;
	}

	public void setRight(IntervalNode<V> right) {
		this.right = right;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	public V getMax() {
		return max;
	}
	
	public void setMax(V max) {
		this.max = max;
	}
	
	@Override
	public String toString() {
		return value.toString();
	}
	
	@Override
	public int hashCode() {
		return value.hashCode();
	}
	
	@Override
	/**
	 * Equal only if values are equal and height are equal
	 */
	public boolean equals(Object obj) {
		
		if(this == obj)return true;
		
		if(obj instanceof IntervalNode){
			IntervalNode<V> o = (IntervalNode<V>)obj;
			
			return o.getValue().equals(value) 
					&& (o.getHeight() == height);
		}
		
		return false;
	}
}
